package cn.com.taurus.controller.getdata;
import cn.com.taurus.entity.*;
import cn.com.taurus.impl.*;
import cn.com.taurus.mapper.FundValueChangeMapper;
import com.google.gson.Gson;
import org.apache.jasper.tagplugins.jstl.core.ForEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * @author zhy
 * @createTime 2021-6-9 14:37:46
 * @remark 获取数据控制层
 * @version 1.0
 */
@Controller
public class GetDataController {
    @Autowired
    FundImpl fundImpl;
    @Autowired
    FundManagerImpl fundManagerImpl;
    @Autowired
    FundCompanyImpl fundCompanyImpl;
    @Autowired
    FundCompanyAndFundManagerImpl fundCompanyAndFundManagerImpl;
    @Autowired
    FundAndFundManagerImpl managerFundImpl;
    @Autowired
    FundValueChangeImpl fundValueChangeImpl;


    //基金数据
    List<Fund> fundData = new ArrayList<>();
    //基金经理数据
    List<FundManager> fundManagerData = new ArrayList<>();
    //基金公司数据
    List<FundCompany> fundCompanyData = new ArrayList<>();
    //基金经理基金公司连接表数据
    List<FundCompanyAndFundManager> fundCompanyAndFundManagerData = new ArrayList<>();
    //基金经理管理的基金的数据
    List<FundAndFundManager> fundAndFundManagerData = new ArrayList<>();
    //基金净值变化数据
    List<FundValueChange> fundValueChangeData = new ArrayList<>();

    //计算获取基金数据的次数
    int getFundDataCount;
    //计算获取基金经理数据的次数
    int getFundManagerDataCount;
    //计算获取基金公司数据的次数
    int getFundCompanyDataCount;
    //计算获取基金净值变化数据的次数
    int getFundValueChangeDataCount;

    ModelAndView modelAndView = new ModelAndView();


    @RequestMapping("/Anonymous/line")
    public String line(){
        return "getdata/line";
    }
    @RequestMapping("/homepage")
    public String homepage() {
        return "datatable/homepage";
    }
    @RequestMapping("/Anonymous/line1")
    public String histogram() {
        return "getdata/line1";
    }

    @RequestMapping("/Anonymous/round2")
    public String round() {
        return "getdata/round2";
    }

    @RequestMapping("/Anonymous/diagram")
    public ModelAndView diagram(String fId){
        modelAndView.addObject("fId",fId);
        modelAndView.setViewName("getdata/diagram");
        return modelAndView;
    }

    @RequestMapping("/Anonymous/january")
    public ModelAndView january(String fId){
        modelAndView.addObject("fId",fId);
        modelAndView.setViewName("getdata/january");
        return modelAndView;
    }
    @RequestMapping("/Anonymous/march")
    public ModelAndView march(String fId){
        modelAndView.addObject("fId",fId);
        modelAndView.setViewName("getdata/march");
        return modelAndView;
    }
    @RequestMapping("/Anonymous/june")
    public ModelAndView june(String fId){
        modelAndView.addObject("fId",fId);
        modelAndView.setViewName("getdata/june");
        return modelAndView;
    }
    @RequestMapping("/Anonymous/year")
    public ModelAndView year(String fId){
        modelAndView.addObject("fId",fId);
        modelAndView.setViewName("getdata/year");
        return modelAndView;
    }
    @RequestMapping("/Anonymous/triennium")
    public ModelAndView triennium(String fId){
        modelAndView.addObject("fId",fId);
        modelAndView.setViewName("getdata/triennium");
        return modelAndView;
    }
    @RequestMapping("/Anonymous/thisyear")
    public ModelAndView thisyear(String fId){
        modelAndView.addObject("fId",fId);
        modelAndView.setViewName("getdata/thisyear");
        return modelAndView;
    }
    @RequestMapping("/Anonymous/maximum")
    public ModelAndView maximum(String fId){
        modelAndView.addObject("fId",fId);
        modelAndView.setViewName("getdata/maximum");
        return modelAndView;
    }
    @RequestMapping("/Anonymous/january2")
    public ModelAndView january2(String fId){
        modelAndView.addObject("fId",fId);
        modelAndView.setViewName("getdata/january2");
        return modelAndView;
    }

    @RequestMapping("/Anonymous/march2")
    public ModelAndView march2(String fId){
        modelAndView.addObject("fId",fId);
        modelAndView.setViewName("getdata/march2");
        return modelAndView;
    }
    @RequestMapping("/Anonymous/june2")
    public ModelAndView june2(String fId){
        modelAndView.addObject("fId",fId);
        modelAndView.setViewName("getdata/june2");
        return modelAndView;
    }
    @RequestMapping("/Anonymous/year2")
    public ModelAndView year2(String fId){
        modelAndView.addObject("fId",fId);
        modelAndView.setViewName("getdata/year2");
        return modelAndView;
    }
    @RequestMapping("/Anonymous/triennium2")
    public ModelAndView triennium2(String fId){
        modelAndView.addObject("fId",fId);
        modelAndView.setViewName("getdata/triennium2");
        return modelAndView;
    }
    @RequestMapping("/Anonymous/thisyear2")
    public ModelAndView thisyear2(String fId){
        modelAndView.addObject("fId",fId);
        modelAndView.setViewName("getdata/thisyear2");
        return modelAndView;
    }
    @RequestMapping("/Anonymous/maximum2")
    public ModelAndView maximum2(String fId){
        modelAndView.addObject("fId",fId);
        modelAndView.setViewName("getdata/maximum2");
        return modelAndView;
    }

    @RequestMapping("/Anonymous/bottom")
    public String fundmanager() {
        return "major/bottom";
    }


    @RequestMapping("/Anonymous/hello.do")
    public String hello(){
        return "getdata/demo";
    }
    @RequestMapping("/Anonymous/hello2.do")
    public String hello2(){
        return "getdata/demo2";
    }
    @RequestMapping("/Anonymous/hello3.do")
    public String hello3(){
        return "getdata/demo3";
    }
    @RequestMapping("/Anonymous/hello4.do")
    public String hello4(){
        return "getdata/demo4";
    }
    @RequestMapping("/Anonymous/hello5.do")
    public String hello5(){
        return "major/oneFund";
    }


    @RequestMapping("/Anonymous/getFundData.do")
    public ModelAndView getFundData(@ModelAttribute Fund fund){
        //获取基金数据的次数自增
        getFundDataCount++;
        fundData.add(fund);
        ModelAndView modelAndView;
        if (getFundDataCount == 20){
            //提交
            fundImpl.addFund(fundData);
            modelAndView = new ModelAndView("getdata/demo");
        }else {
            modelAndView = new ModelAndView("getdata/demo");
        }
        return modelAndView;
    }

    @RequestMapping("/Anonymous/getFundManagerData.do")
    public ModelAndView getFundManagerData(@ModelAttribute FundManager fundManager, String fcId, String managerfId){
        getFundManagerDataCount++;

        //********************************************************************************************************
        //计算首次任职时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //计算 当日 - 任职时间(天)*24*60*60*1000得到的毫秒 数
        String firstOffice =
                sdf.format(date.getTime()-(new Long(fundManager.getFirstOffice())*24*60*60*1000));
        fundManager.setFirstOffice(firstOffice);
        //********************************************************************************************************
        //加入基金经理集合
        fundManagerData.add(fundManager);
        //********************************************************************************************************
        //获取基金公司基金经理连接表的id
        //然后创建对象加入基金经理基金公司连接表集合
        fundCompanyAndFundManagerData.add(
                new FundCompanyAndFundManager
                        (fundCompanyAndFundManagerImpl.selectFcfmId(),fcId,fundManager.getFmId()));
        //********************************************************************************************************
        //********************************************************************************************************
        String managerfIds[] = managerfId.split(","); //将通过split的剪切形成的数组转换成List集合
        for (int i = 0; i < managerfIds.length; i++) {
            fundAndFundManagerData.add(
                    new FundAndFundManager(managerFundImpl.selectFfmId(),managerfIds[i],fundManager.getFmId())); //将单个基金经理所管理的基金放入集合
        }
        //********************************************************************************************************
        ModelAndView modelAndView;
        if (getFundManagerDataCount == 50){
            //将列表的基金经理数据添加到数据库中
            fundManagerImpl.addFundManager(fundManagerData);
            fundCompanyAndFundManagerImpl.addFundCompanyAndFundManager(fundCompanyAndFundManagerData);
            managerFundImpl.addFundAndFundManager(fundAndFundManagerData);
            modelAndView = new ModelAndView("getdata/demo2");
        }else {
            modelAndView = new ModelAndView("getdata/demo2");
        }
        return modelAndView;
    }

    @RequestMapping("/Anonymous/getFundCompanyData.do")
    public ModelAndView getFundcCompanyData(@ModelAttribute FundCompany fundCompany){
        //基金公司个数自增
        getFundCompanyDataCount++;
        fundCompanyData.add(fundCompany);

        ModelAndView modelAndView;
        if (getFundCompanyDataCount == 168){
            fundCompanyImpl.addFundCompany(fundCompanyData);
            modelAndView = new ModelAndView("getdata/demo3");
        }else {
            modelAndView = new ModelAndView("getdata/demo3");
        }
        return modelAndView;
    }

    @RequestMapping("/Anonymous/getFundValueChangeData.do")
    public ModelAndView getFundValueChangeData(@ModelAttribute FundValueChange fundValueChange,Integer length){
        //基金净值变化数据自增
        getFundValueChangeDataCount++;
        fundValueChange.setFvcId(fundValueChangeImpl.selectFvcid());
        fundValueChangeData.add(fundValueChange);

        ModelAndView modelAndView;
        if (getFundValueChangeDataCount == length){
            fundValueChangeImpl.addFundValueChange(fundValueChangeData);
            getFundValueChangeDataCount=0; //计数器清空
            fundValueChangeData.clear(); //删除所有数据
            modelAndView = new ModelAndView("getdata/demo");
        }else {
            modelAndView = new ModelAndView("getdata/demo");
        }
        return modelAndView;
    }
}
